package com.ch01;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
class Dog{
	private String name = "哮天犬";
	private int food = 10;
	public Dog(){};
	public Dog(String name){
		System.out.println("------------");
		this.name = name;
	};
	public static void bark(Dog dog){
		System.out.println(dog+"叫了");
	}
	public int eat(int num){
		System.out.println("吃了"+num+"斤狗粮");
		this.food -= num;
		return this.food;
	}
	public String toString(){
		return this.name;
	}
}
class Print{
	static void print(String s){
		System.out.println(s);
	}
	String toPrint(String s){
		System.out.format("打印了一个值:%s\n", s);
		return "ok";
	}
}
public class MethodRefremceDemo {
	public static void main(String[] args) {
		//方法引用
		//Consumer<String> consumer = System.out::println;
		Consumer<String> consumer = Print::print;
		consumer.accept("输入一个值");
		Print p = new Print();
//		BiFunction<Print, String, String> bf = Print::toPrint;
//		String result = bf.apply(p, "AAAA");
//		System.out.println(result);
		Consumer<String> consumer1 = p::toPrint;
		consumer1.accept("BBBB");
		//print::toPrint;
		//consumer1.accept("输入一个值");
		//静态方法引用
		Consumer<Dog> consumer2 = Dog::bark;
		Dog dog = new Dog();
		consumer2.accept(dog);
		//Function<Integer, Integer> function = dog::eat;
		UnaryOperator<Integer> function1 = dog::eat;
		System.out.println("还剩下"+function1.apply(2)+"斤");
		IntUnaryOperator function2 = dog::eat;
		System.out.println("还剩下"+function2.applyAsInt(3)+"斤");
		BiFunction<Dog, Integer, Integer> eatFun = Dog::eat;
		System.out.println("还剩下"+eatFun.apply(dog, 1)+"斤");
		//构造函数引用
		Supplier<Dog> supplier = Dog :: new;
		System.out.println("创建了新对象"+supplier.get());
		//带参数
		Function<String, Dog> fun = Dog :: new;
		System.out.println("创建了"+fun.apply("旺财"));
	}
}
